# instalacao do qt, e outras infos em
# http://cran.r-project.org/web/packages/qtbase/index.html
source("./Desktop/MO444/proj/word-prediction-mo444/trab-funcoes-fernando.r")



guessNextWord <- function (text) {
  suggestionTable <- getSuggestionList(text)
  #pega os 3 melhores
  max = which.max(suggestionTable)
  max1 = names(max)[1]
  suggestionTable[max] <- NULL
  max = which.max(suggestionTable)
  max2 = names(max)[1]
  suggestionTable[max] <- NULL
  max = which.max(suggestionTable)
  max3 = names(max)[1]
  suggestionTable[max] <- NULL
  return (c(max1, max2, max3))
}

getSuggestionList <-function (text) {
  text = tolower(x=text);
  textSplitted = unlist(strsplit(text, split=" "));
  suggestionTable = vector(mode="list")
  if (length(textSplitted) >= 1) {
    pattern = paste("^", textSplitted[length(textSplitted)], " ", sep="")
    bigrams = grep(pattern, rownames(bigramTable), value = TRUE)
    if (length(bigrams) > 0) {
      for (i in 1:length(bigrams)){
        suggestion = tail(strsplit(bigrams[i],split=" ")[[1]],1)  
        if (suggestion %in% names(suggestionTable)) {suggestionTable[[suggestion]] = suggestionTable[[suggestion]] + bigramTable[bigrams[i],]}
        else {suggestionTable[[suggestion]] = 1}
      }
    }
  }
  
  if (length(textSplitted) >= 2) {
    pattern = paste("^", textSplitted[length(textSplitted)-1], " ", sep="")
    trigrams = grep(pattern, rownames(trigramTable), value = TRUE)
    if (length(trigrams) > 0) {
      for (i in 1:length(trigrams)){
        suggestion = tail(strsplit(trigrams[i],split=" ")[[1]],1)  
        if (suggestion %in% names(suggestionTable)) {suggestionTable[[suggestion]] = suggestionTable[[suggestion]] + trigramTable[trigrams[i],]}
        else {suggestionTable[[suggestion]] = 1}
      }
    }
  }
  if (length(textSplitted) >= 3) {
    pattern = paste("^", textSplitted[length(textSplitted)-2], " ", sep="")
    quadrigrams = grep(pattern, rownames(quadgramTable), value = TRUE)    
    if (length(quadrigrams) > 0) {
      for (i in 1:length(quadrigrams)){
        suggestion = tail(strsplit(quadrigrams[i],split=" ")[[1]],1)  
        if (suggestion %in% names(suggestionTable)) {suggestionTable[[suggestion]] = suggestionTable[[suggestion]] + quadgramTable[quadrigrams[i],]}
        else {suggestionTable[[suggestion]] = 1}
      }
    }
  }
  return (suggestionTable)
}

fixCurrentWord <- function (text, currentWord) {
  suggestionList = getSuggestionList(text)
  for (i in 1:length(names(suggestionList))) {
    dist = adist (currentWord, names(suggestionList)[i])
    dist = 1/dist*10
    #usa a distancia de Levenshtein para computar a distancia entre duas palavras
    suggestionList[[i]] <- dist
  }
  #pega os 3 melhores
  max = which.max(suggestionList)
  max1 = names(max)[1]
  suggestionList[max] <- NULL
  max = which.max(suggestionList)
  max2 = names(max)[1]
  suggestionList[max] <- NULL
  max = which.max(suggestionList)
  max3 = names(max)[1]
  suggestionList[max] <- NULL
  return (c(max1, max2, max3))
}
